//
//  LocationAlertView.m
//  DriverClient
//
//  Created by 吕晶 on 2020/11/13.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "LocationAlertView.h"

@interface LocationAlertView ()

@property (nonatomic, strong) UIView *bgView;

@end

static LocationAlertView *alert;
static dispatch_once_t once;

@implementation LocationAlertView

+ (LocationAlertView *)shareAlert {
    dispatch_once(&once, ^ {
        alert = [[LocationAlertView alloc] init];
    });
    return alert;
}

+ (void)alertRelease {
    once = 0;
    alert = nil;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.frame = CGRectMake(0, 0, ScreenW, ScreenH);
        self.backgroundColor = HexColorA(0x000000, 0.5);
        [self configUI];
    }
    return self;
}

- (void)configUI {
    self.bgView = [[UIView alloc] initWithFrame:CGRectMake(30, (ScreenH - 237) * 0.5, ScreenW - 60, 237)];
    self.bgView.backgroundColor = HexColor(0xffffff);
    self.bgView.layer.masksToBounds = YES;
    self.bgView.layer.cornerRadius = 6;
    [self addSubview:self.bgView];
    
    UIImageView *iconImgView = [[UIImageView alloc] initWithFrame:CGRectMake((self.bgView.nim_width - 60) * 0.5, 30, 60, 60)];
    iconImgView.image = [UIImage imageNamed:@"icon_location_alert"];
    [self.bgView addSubview:iconImgView];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, iconImgView.nim_bottom + 10, self.bgView.nim_width, 17)];
    lab.textColor = HexColor(0x2B2C44);
    lab.font = BoldFontOfSize(16);
    lab.textAlignment = NSTextAlignmentCenter;
    lab.text = @"定位功能未启用";
    [self.bgView addSubview:lab];
    
    UILabel *lab2 = [[UILabel alloc] initWithFrame:CGRectMake(20, lab.nim_bottom + 10, self.bgView.nim_width - 40, 0)];
    lab2.textColor = HexColor(0x9C9DB3);
    lab2.font = [UIFont systemFontOfSize:14];
    lab2.textAlignment = NSTextAlignmentCenter;
    lab2.text = @"请在“设置-隐私-定位服务”开启定位服务，并允许App访问你的位置，否则无法使用。";
    lab2.numberOfLines = 0;
    [self.bgView addSubview:lab2];
    [lab2 sizeToFit];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, self.bgView.nim_height - 54, self.bgView.nim_width, 54)];
    [btn setTitleColor:Color_ThemeColor forState:0];
    [btn setTitle:@"去设置" forState:0];
    btn.titleLabel.font = BoldFontOfSize(16);
    [btn addTarget:self action:@selector(luRuClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:btn];
}

- (void)luRuClick {
    [self disMissView];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
}

- (void)showInView:(UIView *)view {
    if (!view) {
        return;
    }
    [view addSubview:self];
    [view addSubview:self.bgView];
}

- (void)disMissView {
    [self removeFromSuperview];
    [self.bgView removeFromSuperview];
}

@end
